package net.wangds.dymodel.ctrl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.wangds.dymodel.ddl.DyModelDdlComp;
import net.wangds.dymodel.meta.entity.DyModelPropDefEntity;
import net.wangds.dymodel.service.DyModelPropDefOpService;
import net.wangds.jpa.ctrl.AbstractJpaServiceCtrl;
import net.wangds.log.helper.LogHelper;
import net.wangds.web.entrypoint.BaseMultipleResp;
import net.wangds.web.entrypoint.BaseResp;
import net.wangds.web.entrypoint.PagingForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2021/2/19 11:27.
 */
@Api(tags="动态模型属性管理")
@RestController
@RequestMapping("/dymdlprop")
@SuppressWarnings("unused")
public class DyModelPropCtrl extends AbstractJpaServiceCtrl<DyModelPropDefEntity> {

    /**
     * 专家库JPA服务实例.
     */
    @Autowired
    private DyModelPropDefOpService mmServ;

    @Autowired
    private DyModelDdlComp mmDdl;

    @Override
    protected DyModelPropDefOpService fetchService() {
        return mmServ;
    }


    @ApiOperation("根据Id查询")
    @RequestMapping(value = "/dyMdlId/{mdlId}", method = RequestMethod.POST)
    public BaseMultipleResp<DyModelPropDefEntity> byDyMdlId(
            @PathVariable("mdlId")
            String mdlId,
            @RequestBody
            PagingForm page) {
        PageRequest paging = mmConvPf2Pr.convert(page);
        return mmConvPage2Resp.convert(mmServ.findByDyMdlId(mdlId, paging));
    }

    @ApiOperation("添加列")
    @RequestMapping(value = "/addColumn/{propId}", method = RequestMethod.POST)
    public BaseResp<Boolean> createColumn(
            @PathVariable("propId") String propId
    ){
        try {
            mmServ.findById(propId).ifPresent(prop -> mmDdl.addColumn(prop));
        } catch (Exception ex){
            LogHelper.error(ex);
            return BaseResp.of(false).code(-1).msg(ex.getLocalizedMessage());
        }
        return BaseResp.of(true);
    }

    @ApiOperation("修改列")
    @RequestMapping(value = "/alterColumn/{propId}", method = RequestMethod.POST)
    public BaseResp<Boolean> alterColumn(
            @PathVariable("propId") String propId
    ){
        try {
            mmServ.findById(propId).ifPresent(prop -> mmDdl.alterColumn(prop));
        } catch (Exception ex){
            LogHelper.error(ex);
            return BaseResp.of(false).code(-1).msg(ex.getLocalizedMessage());
        }
        return BaseResp.of(true);
    }
}
